package manager

import android.view.View
import event.AckEvent
import protocol.BaseProtocol
import serialport.DeliverPortManager
import serialport.Packages
import task.debug.PostLogTask

object DeliveryManager {
    var isHot = false
    var isSuccess = false

    private val rowHeight = arrayOf(50, 60, 75, 80, 100)

    var rows1 = 26
    var rows2 = 26
    var rows3 = 26
    var rows4 = 26

    fun rowsWithCol(col: Int): Int {
        return when (col) {
            1 -> rows1
            2 -> rows2
            3 -> rows3
            4 -> rows4
            else -> 0
        }
    }

    fun scan(view: View? = null) {
        ReplenishManager.clearAll()
        val proto = BaseProtocol(0x0A).build()
        DeliverPortManager.write(proto)
    }

    fun reset(view: View? = null) {
        val proto = BaseProtocol(0x0C).build()
        DeliverPortManager.write(proto)
    }

    fun heat(waveTime: Int, infrTime: Int) {
        val t1 = waveTime * 1000
        val t2 = infrTime * 1000
        val proto = BaseProtocol(0X07).append7Int4(t1).append7Int4(t2).build()
        DeliverPortManager.write(proto)
    }

    fun pickOpen(view: View? = null) {
        val proto = BaseProtocol(0x04).append7Byte(0x01).build()
        DeliverPortManager.write(proto)
    }

    fun pickClose(view: View? = null) {
        val proto = BaseProtocol(0x04).append7Byte(0x00).build()
        DeliverPortManager.write(proto)
    }

    fun heatOpen(view: View? = null) {
        val proto = BaseProtocol(0x02).append7Byte(0x01).build()
        DeliverPortManager.write(proto)
    }

    fun heatClose(view: View? = null) {
        val proto = BaseProtocol(0x02).append7Byte(0x00).build()
        DeliverPortManager.write(proto)
    }

    fun externPushForward(view: View? = null) {
        val proto = BaseProtocol(0X05).append7Byte(0x00).build()
        DeliverPortManager.write(proto)
    }

    fun externPushRetreat(view: View? = null) {
        val proto = BaseProtocol(0x05).append7Byte(0x01).build()
        DeliverPortManager.write(proto)
    }

    fun innerPushForward(view: View? = null) {
        val proto = BaseProtocol(0x03).append7Byte(0x01).build()
        DeliverPortManager.write(proto)
    }

    fun innerPushRetret(view: View? = null) {
        val proto = BaseProtocol(0x03).append7Byte(0x00).build()
        DeliverPortManager.write(proto)
    }

    fun externUpDownOpen(view: View? = null) {
        val proto = BaseProtocol(0x01).append7Byte(0x00).build()
        DeliverPortManager.write(proto)
    }

    fun externUpDownClose(view: View? = null) {
        val proto = BaseProtocol(0x01).append7Byte(0x01).build()
        DeliverPortManager.write(proto)
    }

    fun delivery(col: Int,
                 row: Int,
                 waveTime: Int,
                 irdaTime: Int,
                 temperature: Int,
                 rejection: Int,
                 onAckCallback: (env: AckEvent ?) -> Unit) {

        PostLogTask.send("出货:$col-$row")

        val buf = BaseProtocol(0x0D)
            .append7Byte(col)
            .append7Byte(row)
            .append7Int4(waveTime * 1000)
            .append7Int4(irdaTime * 1000)
            .append7Int2(temperature)
            .append7Int2(rejection)
            .build()
        isSuccess = false
        DeliverPortManager.write(Packages(buf, onAckCallback))
    }

    fun setRows(col1: Int, col2: Int, col3: Int, col4: Int) {
        val proto = BaseProtocol(0x0E).append7Byte(rowHeight[col1]).append7Byte(
            rowHeight[col2]
        ).append7Byte(rowHeight[col3]).append7Byte(
            rowHeight[col4]
        ).build()
        DeliverPortManager.write(proto)
    }

    fun setOvenTempValue(min: Int, max: Int, timeOut: Int) {
        val proto = BaseProtocol(0x15).append7Int2(min).append7Int2(max).append7Int2(
            timeOut
        ).build()
        DeliverPortManager.write(proto)
    }

    fun queryRowHeight() {
        val proto = BaseProtocol(0x17).build()
        DeliverPortManager.write(proto)
    }

    fun indexWithRowHeight(height: Int): Int {
        when (height) {
            50 -> return 1
            60 -> return 2
            75 -> return 3
            80 -> return 4
            100 -> return 5
        }
        throw Exception("未知高度")
    }

    fun rowsWithHeight(height: Int): Int {
        when (height) {
            50 -> return 26
            60 -> return 22
            75 -> return 17
            80 -> return 16
            100 -> return 13
        }
        throw Exception("未知高度")
    }

}